import enquireJs from 'enquire.js'
export function timeFix() {
  const time = new Date()
  const hour = time.getHours()
  return hour < 9 ? '早上好' : hour <= 11 ? '上午好' : hour <= 13 ? '中午好' : hour < 20 ? '下午好' : '晚上好'
}

export function isDef(v) {
  return v !== undefined && v !== null
}

/**
 * Remove an item from an array.
 */
export function remove(arr, item) {
  if (arr.length) {
    const index = arr.indexOf(item)
    if (index > -1) {
      return arr.splice(index, 1)
    }
  }
}

export function isRegExp(v) {
  return _toString.call(v) === '[object RegExp]'
}

export function enquireScreen(call) {
  const handler = {
    match: function() {
      call && call(true)
    },
    unmatch: function() {
      call && call(false)
    }
  }
  enquireJs.register('only screen and (max-width: 767.99px)', handler)
}

const _toString = Object.prototype.toString

/**
 * 过滤对象中为空的属性
 * @param obj
 * @returns {*}
 */
export function filterObj(obj) {
  if (!(typeof obj === 'object')) {
    return
  }

  for (const key in obj) {
    if (Object.prototype.hasOwnProperty.call(obj, key) && (obj[key] == null || obj[key] === undefined || obj[key] === '')) {
      delete obj[key]
    }
  }
  return obj
}

/**
 * 触发 window.resize
 */
export function triggerWindowResizeEvent() {
  const event = document.createEvent('HTMLEvents')
  event.initEvent('resize', true, true)
  event.eventType = 'message'
  window.dispatchEvent(event)
}

/**
 * 获取文件服务访问路径
 * @param avatar
 * @param subStr
 * @returns {*}
 */
export function getFileAccessHttpUrl(avatar, subStr) {
  if (!subStr) subStr = 'http'
  try {
    if (avatar && avatar.startsWith(subStr)) {
      return avatar
    } else {
      if (avatar && avatar.length > 0 && avatar.indexOf('[') === -1) {
        return process.env.VUE_APP_API_BASE_URL + avatar
      }
    }
  } catch (err) {
    return
  }
}

// 根据后台返回的二进制流下载文件
export function downloadFile(data, fileName) {
  if (typeof window.navigator.msSaveBlob !== 'undefined') {
    window.navigator.msSaveBlob(new Blob([data], { type: data.type }), fileName)// { type: 'application/vnd.ms-excel' }
  } else {
    const url = window.URL.createObjectURL(new Blob([data], { type: data.type }))// { type: 'application/vnd.ms-excel' }
    const link = document.createElement('a')
    link.style.display = 'none'
    link.href = url
    link.setAttribute('download', fileName)
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link) // 下载完成移除元素
    window.URL.revokeObjectURL(url) // 释放掉blob对象
  }
}

//  根据后台返回的二进制流转换为图片
export function toBase64(data) {
  return new Promise((resolve, reject) => {
    const blob = new Blob([data], { type: data.type }) // 类型一定要写！！！
    const reader = new FileReader()
    reader.readAsDataURL(blob)
    reader.onload = () => resolve(reader.result)
    reader.onerror = (error) => reject(error)
  })
}

// 判断值是否为空
export function isEmpty(val) {
  return val === '' || val === null || val === undefined
}

export function readAsTextOnBlob(response) {
  return new Promise(function(resolve, reject) {
    const reader = new FileReader()
    reader.readAsText(response.data, 'utf-8')
    reader.onload = function() {
      const data = JSON.parse(reader.result)
      resolve(data)
    }
    reader.onerror = function(error) {
      reject(error)
    }
  })
}

// 判断是否为字符串JSON对象
export function isJsonString(str) {
  try {
    if (typeof JSON.parse(str) === 'object') {
      return true
    }
  } catch (e) {}
  return false
}
